package com.wf.course.web.controller.member;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.member.MemberAddress;
import com.wf.course.service.IMemberAddressService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 用户地址Controller
 */
@RequestMapping("memberAddress")
@Controller
public class MemberAddressController {

	@Autowired
	private IMemberAddressService addressService;

	/**
	 * 用户地址列表
	 * @param pageNo
	 * @param pageSize
	 * @param userName
	 * @param model
	 * @return
	 */
	@RequestMapping("memberAddressList")
	public String chapterList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "telephone", required = false) String telephone, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("telephone", telephone);
		PageBean<MemberAddress> pageBean = new PageBean<MemberAddress>(pageNo, pageSize, null, cons);
		pageBean.setUrl("memberAddress/memberAddressList");
		this.addressService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "member/memberAddressList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			MemberAddress memberAddress = addressService.queryMemberAddressById(id);
			model.addAttribute("memberAddress", memberAddress);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		
		return "member/memberAddressEdit";
	}

	/**
	 * 新增和修改
	 * @param MemberAddress
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改用户地址信息")
	@RequestMapping(value = "MemberAddressEdit" , produces = "text/html;charset=UTF-8")
	public String MemberAddressEdit(@ModelAttribute MemberAddress memberAddress,HttpServletRequest request) {
		
		addressService.saveOrUpdateMemberAddress(memberAddress);
		
		return "redirect:/memberAddress/memberAddressList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除用户地址信息")
	@RequestMapping(value = "remove/{id}")
	public String removeMemberAddress(@PathVariable("id") Integer id) {
		
		addressService.deleteById(id);
		
		return "redirect:/memberAddress/memberAddressList";
	}
	

}
